/*
   Copyright (c) 2019 Shanghai Xuanzi Technology Co. Ltd https://xuanzi.ltd
   XZMindMap is licensed under the Mulan PSL v1.
   You can use this software according to the terms and conditions of the Mulan PSL v1.
   You may obtain a copy of Mulan PSL v1 at:
      http://license.coscl.org.cn/MulanPSL
   THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
   PURPOSE.
   See the Mulan PSL v1 for more details.

*/

package xuanzi.commons.graphics;

 
/**
 * 颜色相关的工具
 * @author 彭立铭
 *
 */
public class Color {

	public static final int ERROR = 0xEEe21212;
	public static final int FOCUS = 0xEE12a1e2;

	public Color() {
		// TODO Auto-generated constructor stub
	}
	
	public static String toHex(String color){
		if(color.startsWith("#"))
			return color;
		if(color.toLowerCase().startsWith("rgba"))
			return rgba2Hex(color);
		
		if(color.toLowerCase().startsWith("rgb"))
			return rgb2Hex(color);
		return "#FFFFFFFF";
	}
	
	public static String toRGB(String color){
		
		return hex2rgb(color);
	}
	
	public static String toRGBA(String color){
		
		return hex2rgba(color);
	}
	
	public static String int2hex(int color){
		//Color.toRGBA(
		String value = Integer.toHexString(color);
		if(value.length()<8)
			value = "0"+value;
		return "#"+value;
	}
	
	public static String int2RGBA(int color){
		 
		String hex = (int2hex(color));
		if(hex.length()>6) {
			//System.out.println(hex);
			String a = hex.substring(1,3); 
			String r = hex.substring(3,5); 
			String g = hex.substring(5,7);  
			String b = hex.substring(7,9); 
			float aa = Integer.parseInt(a,16)/255f;
			
			String result = "rgba("+Integer.parseInt(r,16)+","+Integer.parseInt(g,16)+","+Integer.parseInt(b,16)+","+aa+")";
			//console.log(result);
			return result;
		}
		return "#000000";
		
	}
	
	public static int hex2int(String hex){
		int a = 0xFF;
		int color = 0;
		String shex = hex;
		if(shex.startsWith("#"))
			shex = shex.substring(1);
		if(shex.length()>6){
			String alpha = shex.substring(0,2);
			a = Integer.parseInt(alpha, 16);
			shex = shex.substring(2);
			color = Integer.parseInt(shex, 16);
			color += 0x1000000*a;
			//Log.log(alpha+"-"+shex+" "+int2hex(color)+" "+int2RGBA(color));
			
		}else{
			color = Integer.parseInt(shex, 16);
		}
		
		
		//Log.i(Integer.toHexString(color + a*0x1000000),Integer.toHexString(color));
		//Log.i(hex,toRGBA(hex));
		return color;
	}

	public static native String rgb2Hex(String rgb) /*-{
		 var ds = rgb.split(/\D+/);
		// console.log(ds[1]);
		 var decimal = Number(ds[1]) * 65536 + Number(ds[2]) * 256 + Number(ds[3]);
		  decimal = decimal.toString(16);
		 // console.log(decimal);
		 return '#ff'+(decimal.length<6?decimal:decimal);
	}-*/;
	
	public static native String rgba2Hex(String rgb) /*-{
		 var ds = rgb.split(/\D+/);
		 var decimal = Number(ds[1]) * 65536 + Number(ds[2]) * 256 + Number(ds[3]);
		 var a = (Number(ds[4])*255).toString(16);
		 decimal = decimal.toString(16);
		 return '#'+(a.length==1?'0'+a:a)+(decimal.length<6?'0'+decimal:decimal);
	 
	}-*/;
	
	public static native String hex2rgb(String hex) /*-{
		var r = hex.substring(1,3);
		var g = hex.substring(3,5); 
		var b = hex.substring(5,7); 
		return "rgb("+parseInt(r,16)+","+parseInt(g,16)+","+parseInt(b,16)+")";

	}-*/;
	
	public static native String hex2rgba(String hex) /*-{
		//var r = hex.substring(1,3);
		var a = hex.substring(1,3); 
		var r = hex.substring(3,5); 
		var g = hex.substring(5,7);  
		var b = hex.substring(7,9); 
		var result = "rgba("+parseInt(r,16)+","+parseInt(g,16)+","+parseInt(b,16)+","+(parseInt(a,16)/255)+")";
		//console.log(result);
		return result;
	}-*/;
	
}
